<template>
	<view class="uni-px-4">
		<uni-easyinput class="uni-my-4" v-model="password" type="password" placeholder="請輸入新的密碼"></uni-easyinput>
		<uni-easyinput class="uni-my-4" v-model="repetPassword" type="password" placeholder="請再次輸入密碼"></uni-easyinput>
		<button @click="changePassword">完成</button>
	</view>
</template>

<script>
import {
	httpUserEditPassword
} from '@/utils/api.js'
export default {
	data() {
		return {
			password: '',
			repetPassword: '',
			userId: ''
		}
	},
	onLoad() {
		const user = getApp().globalData.user
		this.userId = user.userId || user.id
	},
	methods: {
		async changePassword() {
			if (this.password === '' || this.repetPassword === '') {
				uni.showToast({
					title: '輸入不能爲空',
					icon: 'none'
				})
				return false
			}
			if (this.password != this.repetPassword) {
				uni.showToast({
					title: '密碼前後不一致',
					icon: 'none'
				})
				return false
			}
			const { code, message } = await httpUserEditPassword(this.userId, this.password)
			if (code === 200) {
				uni.showToast({
					title: '更改成功',
					icon: 'none'
				})
			} else {
				uni.showToast({
					title: message,
					icon: 'none'
				})
			}
		}
	}
}
</script>

<style></style>